/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import request from '@/utils/request';
import { isArray } from 'lodash';
import type { ResponseItem } from 'mock/utils';
import type { UploadAuthListItem } from '@/pages/CityXLS/UploadAuthProps';
import { message } from 'antd';

const apis = {
  htUpload: {
    // 百事
    list: '/NewRetailSecretLogin/officialList',
    add: '/NewRetailSecretLogin/officialHtAddV2',
    del: '/NewRetailSecretLogin/delBSItem',
  },
  ytUpload: {
    // 圆通
    list: '/NewRetailSecretLogin/officialYTList',
    add: '/NewRetailSecretLogin/officialYTAdd',
    del: '/NewRetailSecretLogin/delYTItem',
  },
  jtUpload: {
    //极兔
    list: '/JtBranch/list',
    add: '/JtBranch/add',
    del: '/JtBranch/del',
  },
};

export async function getHTStations(params: any, brand: any): Promise<any> {
  const url = apis[brand].list;
  return request(url, {
    method: 'POST',
    data: params,
  }).then(({ data }) => {
    const { list, page, count } = data;
    if (isArray(list) && list.length > 0) {
      return {
        total: count,
        success: true,
        data: list,
        current: page,
      };
    }
    return {};
  });
}

export async function addHTStation(params: any, brand: any): Promise<any> {
  const url = apis[brand].add;
  return request(url, {
    method: 'POST',
    data: params,
  });
}

export async function delHTStation(params: any, brand: any): Promise<any> {
  const url = apis[brand].del;
  return request(url, {
    method: 'POST',
    data: params,
  });
}

export async function getUploadAuthList(params: {
  page: number;
  size: number;
  account?: string;
}): Promise<any> {
  return request<
    ResponseItem & { data: { list: UploadAuthListItem[]; page: string; count: string } }
  >('/Authorization/authorizationList', {
    method: 'POST',
    data: params,
  }).then(({ data }) => {
    const { list, page, count } = data;
    if (isArray(list) && list.length > 0) {
      return {
        total: count,
        success: true,
        data: list,
        current: page,
      };
    }
    return {};
  });
}

export async function exportExcel() {
  return request<ResponseItem & { data: string }>('/LogisticsPortSurvey/surveyDataDerive', {
    method: 'GET',
  }).then((res) => {
    if (res.code == 0) {
      window.location.href = res.data;
    }
  });
}
/**
 * 新增共配接口上传授权
 *  */
export async function addGpAuth(params: { account: string; phone?: string }) {
  return request<ResponseItem>('/Authorization/authorizationAdd', {
    method: 'POST',
    data: params,
  });
}
/**
 * 删除共配接口上传授权
 *  */
export async function deleteGpAuth(params: { id: string }) {
  return request<ResponseItem>('/Authorization/authorizationDel', {
    method: 'POST',
    data: params,
  });
}
/**
 * 共配上传，导出接口
 *  */
export async function gpExportExcel(params: { account: string }) {
  return request<ResponseItem & { data: string }>('/Authorization/authorizationExport', {
    method: 'POST',
    data: params,
  }).then((res) => {
    if (res.code == 0) {
      if (res.data) {
        window.location.href = res.data;
      } else {
        message.warning('暂无导出链接');
      }
    } else {
      message.error(`导出失败：${res.msg}`);
    }
  });
}
